#!/bin/sh


declare -A GitRepositoryMap
GitRepositoryMap["note"]="https://gitee.com/zhouzmo/note.git"
GitRepositoryMap["av"]="https://gitee.com/zhouzmo/learn_audio_video_development.git"
# GitRepositoryMap["temp"]="https://gitee.com/zhouzmo/learn_temp.git"
GitRepositoryMap["c"]="https://gitee.com/zhouzmo/learn_c.git"
GitRepositoryMap["ai"]="https://gitee.com/zhouzmo/learn_artificial_intelligence.git"

GitRepositoryMap["cpp"]="https://gitee.com/zhouzmo/learn_cpp.git"
GitRepositoryMap["py"]="https://gitee.com/zhouzmo/learn_python.git"
GitRepositoryMap["sh"]="https://gitee.com/zhouzmo/learn_shell.git"



FSTR="[zmo:::]"
RED_S="\033[31m"
RED_E="\033[0m"

GEN_S="\033[32m"
GEN_E="\033[0m"

YEL_S="\033[33m"
YEL_E="\033[0m"


# echo -e "${FSTR}${RED_S}  red ${RED_E}"
# echo -e "${FSTR}${GEN_S}  green ${GEN_E}"

# get and check the bash version
bash_version=$(bash --version | head -n1 | awk '{print $4}' | cut -d. -f1)
# echo "bash version:$bash_version"
if [ "$bash_version" -lt 4 ]; then
    echo "${FSTR}此脚本需要bash版本4.0及以上才能运行，请升级bash版本后再尝试。"
    exit 1
fi


# If something happens wrong, it will exit.
# set -e



func_pull_and_push() {
    git pull
    if [ $? -ne 0 ]; then
        echo -e "${FSTR}${RED_S}拉取代码出现错误，可能存在合并冲突，请手动解决后再继续执行脚本。${RED_S}"
        return -1
    fi
    git add *
    git commit -am "😀 $CURRENT_DATE"
    echo -e "${FSTR}${GEN_S}commit完毕，准备推送代码 ${GEN_E}"
    git push
    if [ $? -eq 0 ]; then
        echo -e "${FSTR}${GEN_S}流程结束，完成提交。${GEN_E}"
         return 0
    else
        echo -e "${FSTR}${RED_S}推送代码出现错误，请检查原因并解决。${RED_S}"
        return -2
    fi
}

func_extract_repo_name() {
    local url="$1"
    local base_name=$(basename "$url")
    # 使用 sed 命令删除.git后缀（如果有的话），得到最终的仓库名
    local repo_name=$(echo "$base_name" | sed 's/\.git$//')
    echo "$repo_name"
}



BASE_DIR_GIT_PROJECT="/"



CURRENT_PATH=$(cd "$(dirname "$0")";pwd)
CURRENT_DATE=$(date +'%Y-%m-%d %H:%M:%S')

# check param
if [ $# -lt 2 ]; then
    echo -e "${FSTR}${RED_S} usage: code_git.sh [repositoryKey] [operate]${RED_E}"
    for key in ${!GitRepositoryMap[*]};do
        RepositoryDir=$(func_extract_repo_name "${GitRepositoryMap[$key]}")
        echo -e "$key \t ${RepositoryDir}"
    done
    exit 1
fi


INPUT_GIT_REPOSITORY_KEY="$1"
INPUT_GIT_OPERATE="$2"

key_exists=0
for key in ${!GitRepositoryMap[@]};do
    if [ "${key}" = "${INPUT_GIT_REPOSITORY_KEY}" ] || [ "${INPUT_GIT_REPOSITORY_KEY}" = "all" ]; then
        key_exists=1
        break
    fi
done
if [ $key_exists -eq 0 ]; then
    echo -e "${FSTR}${RED_S}Error: The specified repository key '$INPUT_GIT_REPOSITORY_KEY' does not exist in GitRepositoryMap.${RED_E}"
    exit 1
fi
if [ "${INPUT_GIT_OPERATE}" != "clone" ] && [ "${INPUT_GIT_OPERATE}" != "push" ]; then
    echo -e "${FSTR}${RED_S}Error: The operate parameter must be 'clone' or 'push'.${RED_E}"
    exit 1
fi






# clone or push the code from the git repository
for key in ${!GitRepositoryMap[@]};do
    
    if [ "${key}" = "${INPUT_GIT_REPOSITORY_KEY}" ] || [ "${INPUT_GIT_REPOSITORY_KEY}" = "all" ]; then

        # ========== clone ===========
        if [ "${INPUT_GIT_OPERATE}" = "clone" ]; then
            echo "=================================="
            echo "${FSTR}clone from RepositoryURL:  ${GitRepositoryMap[$key]}"
            echo "=================================="

            cd "${CURRENT_PATH}/${BASE_DIR_GIT_PROJECT}"
            # echo "=== ${CURRENT_PATH}/${BASE_DIR_GIT_PROJECT} ==="
            git clone ${GitRepositoryMap[$key]}
            if [ $? -ne 0 ]; then
                echo -e "\033[31m ${FSTR}Error: git clone for ${GitRepositoryMap[$key]} failed. \033[0m"
            fi
            cd -
        fi

        # ========== push ===========
        if [ "${INPUT_GIT_OPERATE}" = "push" ]; then
                # echo "=================================="
                # echo "${FSTR}push to RepositoryURL:  ${GitRepositoryMap[$key]}"
                # echo "=================================="
                 # echo -e "${FSTR}${GEN_S}  green ${GEN_E}"
                RepositoryDir=$(func_extract_repo_name "${GitRepositoryMap[$key]}")
                cd "${CURRENT_PATH}/${BASE_DIR_GIT_PROJECT}${RepositoryDir}"
                echo -e "${FSTR}${YEL_S} cd ${CURRENT_PATH}/${BASE_DIR_GIT_PROJECT}${RepositoryDir} ${YEL_E}"
                func_pull_and_push
                if [ $? -eq 0 ]; then
                    echo -e "${FSTR}${GEN_S} push success  ${GEN_E}"
                else
                    echo -e "${FSTR}${GEN_S}The number is greater than 10.${GEN_E}"
                fi
                cd -
        fi
        # ==========  ===========
    fi

done

